package com.efrei.schoolmanager.tests.model.hibernate;

import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.efrei.schoolmanager.exceptions.LogicException;
import com.efrei.schoolmanager.model.hibernate.HibernateUtil;
import com.efrei.schoolmanager.model.hibernate.dao.ClasseDAO;
import com.efrei.schoolmanager.model.obj.Classe;
import com.efrei.schoolmanager.model.obj.Eleve;

/**
 * test pour comprendre comment fonctionne Hibernate
 * @author pierrick
 */
public class TestHibernate {

	@SuppressWarnings("unchecked")
	public static void test_multiTransaction() throws HibernateException, LogicException {
		Session s1, s2;
		Transaction t1, t2;
		
		s1 = HibernateUtil.getSession();
		t1 = s1.beginTransaction();
		System.out.println("transaction 1 ok");
		
		System.out.println("stats: "+s1.getStatistics());
		System.out.println("localstatus: "+t1.getLocalStatus());
		
		List<Eleve> eleves = (List<Eleve>) s1.createQuery("FROM Eleve").list();
		System.out.println(eleves.size()+" eleves");
		
		try {
			s2 = HibernateUtil.getSessionFactory().openSession();
			System.out.println("creation de s2 ok");
			t2 = s2.beginTransaction();
			System.out.println("creation t2 ok");
			
			System.out.println("stats: "+s2.getStatistics());
			System.out.println("localstatus: "+t2.getLocalStatus());
			
			List<Eleve> eleves2 = (List<Eleve>) s1.createQuery("FROM Eleve").list();
			System.out.println(eleves2.size()+" eleves");
			
			t2.rollback();
			System.out.println("fermeture t2 ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			t1.rollback();
			System.out.println("fermeture t1 ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("rawtypes")
	public static void displayList(List list, String objName) {
		if(list==null) {
			System.err.println("List null for "+objName);
			return;
		}
		System.out.println("\n\nDisplay "+list.size()+" "+objName);
		for (Object obj : list) {
			System.out.println(obj);
		}
	}
	
	public static void test_refreshDAO() {
		Session session;
		try {
			session = HibernateUtil.getSession();
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		
		Transaction tx = session.beginTransaction();
		List<Classe> classes = null;
		try {
			//et reutilisation de eleve
			//init du DAO
			ClasseDAO classeDao = new ClasseDAO(session);
			//recuperation des classes
			classes = classeDao.getClasses();
			//et utilisation de la classe
			for (Classe classe : classes) {
				System.out.println(classe);
			}
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		
		//pour le reattach
		Set<Eleve> eleves = null;
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			ClasseDAO classeDao = new ClasseDAO(session);
			//reprise avec la 1e classe
			Classe classe = classes.get(0);
			System.out.println("Avec "+classe+", ses eleves:");
			try {
				classeDao.reattachToSession(classe);
				//get eleves
				eleves = classe.getEleves();
				System.out.print("NbEleves: ");
				System.out.flush();
				System.out.println(eleves.size());
			} catch (Exception e) {
				System.out.println("Lazy ;)");
				throw e;
			}
			for (Eleve eleve : eleves) {
				System.out.println(eleve);
			}
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		
		System.out.println("Fin de la recuperation :)");
	}
	
	public static void main(String[] args) {
		try {
			test_multiTransaction();
		} catch (Exception e) {
			System.out.println("catch global:");
			e.printStackTrace();
		}
	}
}
